﻿using System;
using System.Web.Services;
using VMWorkshop.Domain;
using VMWorkshop.Web.Models;
using VMWorkshop.Web.Models.ViewModels;

namespace VMWorkshop.Web
{
    public partial class Ajax : System.Web.UI.Page
    {
        private AjaxViewModel _vm = null;

        private void Page_Init(object sender, EventArgs e)
        {
            this._vm = new AjaxViewModel(new ProductRepository()); 
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.grdProducts.DataSource = this._vm.Products;
            this.grdProducts.DataBind();
        }

        [WebMethod]
        public static ProductEditView GetProductById(int productId)
        {
            AjaxViewModel vm = new AjaxViewModel(new ProductRepository());
            return vm.GetProductById(productId);
        }

        [WebMethod]
        public static void PersistChanges(ProductEditView editView)
        {
            AjaxViewModel vm = new AjaxViewModel(new ProductRepository());
            vm.PersistChanges(editView);
        }
    }
}
